/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package org.aroundme.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aroundme.model.Place;
import org.aroundme.model.XMLResponseEncoder;
/**
 * This class is responsible for returning a response to the client. Every
 * message that is send back goes over this class here
 */
public class ResponseController {
	private static Log log = LogFactory.getLog(ResponseController.class);
	private XMLResponseEncoder responseEncoder = null;
	private HttpServletResponse response = null;

	public ResponseController() {
	}

	/**
	 * returns the response to the client
	 * @param xmlResponseString
	 * @throws IOException
	 */
	private void returnResponse(String xmlResponseString) throws IOException {
		//return to client
		response.setContentType("application/xml");
//		response.setCharacterEncoding("iso-8859-1");
		response.setStatus(HttpServletResponse.SC_OK);
		response.setContentLength(xmlResponseString.length());
		
		if(response == null || xmlResponseString == null)
			throw new NullPointerException("response object or string was null");
		else
			response.getWriter().println(xmlResponseString);
		log.info("Return xml: " + xmlResponseString);
	}
	
	/**
	 * sends an alert back to the client
	 * @param alertType
	 * @param message
	 */
	public void sendAlert(String alertType, String message){
		String responseStr = responseEncoder.encodeAlert(alertType, message);
		try {
			returnResponse(responseStr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Sends a list of places back to the client
	 * @param places
	 */
	public void sendPlaces(List<Place> places){
		String responseStr = responseEncoder.encodePlacesToXML(places);
		try {
			returnResponse(responseStr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/*
	 * Getters and Setters
	 */
	
	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public XMLResponseEncoder getResponseEncoder() {
		return responseEncoder;
	}

	public void setResponseEncoder(XMLResponseEncoder responseEncoder) {
		this.responseEncoder = responseEncoder;
	}

}
